package com.example.administrator.xxnapp.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.xxnapp.R;

/**
 * Created by Administrator on 2018/5/24.
 */

public class EmptyDataLayout extends LinearLayout implements View.OnClickListener{
    private int nullIconRes,errorIconRes;
    private String nullString,errorString;
    private ImageView mIVIcon;
    private TextView tvTitle;
    private boolean isError;

    public EmptyDataLayout(Context context) {
        this(context,null);
    }

    public EmptyDataLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public EmptyDataLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context,R.layout.empty_list_data_layout,this);

        mIVIcon = findViewById(R.id.empty_list_data_layout_IV);
        tvTitle = findViewById(R.id.empty_list_data_layout_TV_Title);

        initAttrs(context,attrs);

        if (nullString == null){
            nullString = "";
        }
        if (errorString == null){
            errorString = "";
        }

        setOnClickListener(this);

        setError(false);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.EmptyDataLayout);
        nullIconRes = array.getResourceId(R.styleable.EmptyDataLayout_nullIcon,R.mipmap.empty_data_icon);
        errorIconRes = array.getResourceId(R.styleable.EmptyDataLayout_errorIcon,R.mipmap.empty_data_icon);
        nullString = array.getString(R.styleable.EmptyDataLayout_nullString);
        errorString = array.getString(R.styleable.EmptyDataLayout_errorString);
        array.recycle();
    }


    @Override
    public void onClick(View view) {
        if (isError){
            //加载出错
            if (onEmptyLayoutClickListener != null){
                onEmptyLayoutClickListener.onEmptyError();
            }
        }else {
            //空数据
            if (onEmptyLayoutClickListener != null){
                onEmptyLayoutClickListener.onEmptyEmpty();
            }
        }
    }



    public boolean isError() {
        return isError;
    }

    public void setError(boolean error) {
        isError = error;
        mIVIcon.setImageResource(isError?errorIconRes:nullIconRes);
        tvTitle.setText(isError?errorString:nullString);
    }

    private OnEmptyLayoutClickListener onEmptyLayoutClickListener;

    public void setOnEmptyLayoutClickListener(OnEmptyLayoutClickListener onEmptyLayoutClickListener) {
        this.onEmptyLayoutClickListener = onEmptyLayoutClickListener;
    }

    public interface OnEmptyLayoutClickListener{
        void onEmptyError();
        void onEmptyEmpty();
    }
}
